<?php
/*-------------------------------------------------------------------------\
|
|  ========================================================
|  Fusion Registry GPL
|  Copyright (C) 2007 Fusion Scripts
|
|  This program is free software; you can redistribute it and/or
|  modify it under the terms of the GNU General Public License
|  as published by the Free Software Foundation; either version 2
|  of the License, or (at your option) any later version.
|
|  This program is distributed in the hope that it will be useful,
|  but WITHOUT ANY WARRANTY; without even the implied warranty of
|  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
|  GNU General Public License for more details.
|
|  You should have received a copy of the GNU General Public License
|  along with this program; if not, write to the Free Software
|  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
|  ========================================================
|
|  Email: info@fusionscripts.com
|  Web-Site: http://www.fusionscripts.com/
|
|  $Id: ad_categories.php 12 2008-08-03 05:54:08Z registry.fusion $
|
\-------------------------------------------------------------------------*/

/**
 * This is the admin interface for Fusion Registry modification categories
 *
 * @package Fusion Registry
 * @subpackage admin
 * @version $Id: ad_categories.php 12 2008-08-03 05:54:08Z registry.fusion $
 */

if ( ! defined( 'AD_REGISTRY_CALLED' ) )
{
    print "<h1>Incorrect access</h1>You cannot access this file directly.";
    exit();
}

/**
 * Fusion Registry requires
 */
require_once(AD_REGISTRY_PATH.'ad_registry_loader.php');

/**
 * Class ad_registry_categories deals with config & management of modification categories
 *
 * @package Fusion Registry
 * @subpackage admin
 */
class ad_registry_categories
{
    var $ipsclass;

    /** @var string */
    var $base_url;

    /** @var object */
    var $ad_registry_loader;

    /**
     * Intialization of ad_registry_categories
     *
     * Another traffic cop function, there should be minimal procedural code
     * here.
     */
    function auto_run()
    {
        // Kill globals
        $tmp_in = array_merge( $_GET, $_POST, $_COOKIE );

        foreach ( $tmp_in as $k => $v )
        {
            unset($$k);
        }

        // Initialize ad_registry_loader
        $this->ad_registry_loader = new ad_registry_loader();
        $this->ad_registry_loader->ipsclass =& $this->ipsclass;
        $this->ad_registry_loader->auto_run(&$this);

        switch($this->ipsclass->input['code'])
        {
            case 'modify_cat_menu':
                $this->modify_cat_menu();
                break;

            case 'modify_cat':
                $this->modify_cat();
                break;

            case 'new_cat':
                $this->new_cat();
                break;

            case 'delete_cat_menu':
                $this->delete_cat_menu();
                break;

            case 'delete_cat':
                $this->delete_cat();
                break;

            default:
                $this->main_menu();
                break;
        }
    }


    /**
     * Delete a modification category
     */
    function delete_cat()
    {
        // Grab the category title being deleted
        $this->ipsclass->DB->query("SELECT title FROM ibf_registry_categories WHERE id='".$this->ipsclass->input['id']."'");
        $category = $this->ipsclass->DB->fetch_row();

        // Do we have a valid source?
        if ($this->ipsclass->input['id'] == "")
        {
            $this->ipsclass->admin->error("Could not determine the source garage category ID.");
        }

        // How about a valid destination?
        if ($this->ipsclass->input['cat_move_id'] == "")
        {
            $this->ipsclass->admin->error("Could not determine the destination garage category ID.");
        }

        // Move the existing modifications
        $this->ipsclass->DB->query("UPDATE ibf_registry_mods SET category_id='".$this->ipsclass->input['cat_move_id']."' WHERE category_id='".$this->ipsclass->input['id']."'");

        // Bye-Bye... delete the category
        $this->ipsclass->DB->query("DELETE FROM ibf_registry_categories WHERE id='".$this->ipsclass->input['id']."'");

        // Rebuild cache
        $this->update_cache();

        $text = "Deleted Fusion Registry Category '{$category['title']}'";
        $url = 'act='.REGISTRY_URL.'&section=components&menu=categories';
        $time = 2;

        // Big brother is watching :)
        $this->ipsclass->admin->save_log($text);

        // End of the show, c'ya
        $this->ipsclass->admin->redirect( $url, $text, 0, $time );
    }


    /**
     * Build the HTML form for creating/modifying a modification category
     */
    function delete_cat_menu()
    {
        $cat_array = array();

        // Do we have a valid category id to modify?
        if ($this->ipsclass->input['id'] == "")
        {
            $this->ipsclass->admin->error("Could not determine the Fusion Registry category ID to delete.");
        }

        // Grab the current categories
        $this->ipsclass->DB->query("SELECT id, title, image_id FROM ibf_registry_categories ORDER BY `title` ASC");

        // Make sure we have more than 1 category
        if ($this->ipsclass->DB->get_num_rows() < 2)
        {
            $this->ipsclass->admin->error("Can not remove this Fusion Registry category, please create another before attempting to remove this one");
        }

        // Build our array of data
        while ( $registry_cat = $this->ipsclass->DB->fetch_row() )
        {
            if ($registry_cat['id'] == $this->ipsclass->input['id'])
            {
                $name = $registry_cat['title'];
                continue;
            }

            $cat_array[] = array( $registry_cat['id'], $registry_cat['title']);
        }

        $this->ipsclass->html .= $this->ipsclass->skin_acp_global->information_box( "Removing Fusion Registry category '$name'", "Before we remove this Fusion Registry category, we need to determine what to do with any modifications that may be left in this category.".'<br />&nbsp;' ) . "<br >";

        // Build the HTML
        $this->ipsclass->html .= $this->ipsclass->adskin->start_form( array( 1 => array( 'code', 'delete_cat'           ),
                                                                             2 => array( 'act' , REGISTRY_URL           ),
                                                                             3 => array( 'id'  , $this->ipsclass->input['id'] ),
                                                                             4 => array( 'menu', 'categories'           ),
                                                                             5 => array( 'section', 'components'        ),
                                                                    )      );

        $this->ipsclass->adskin->td_header[] = array( "&nbsp;"  , "40%" );
        $this->ipsclass->adskin->td_header[] = array( "&nbsp;"  , "60%" );

        $this->ipsclass->html .= $this->ipsclass->adskin->start_table( 'Required' );

        $this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<b>Category to remove: </b>" , $name ) );

        $this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<b>Move all <i>existing modifications in this category</i> to which category?</b>" ,
                                                                             $this->ipsclass->adskin->form_dropdown( "cat_move_id", $cat_array )
                                                                    )      );

        $this->ipsclass->html .= $this->ipsclass->adskin->end_form("Move modifications and delete this category");

        $this->ipsclass->html .= $this->ipsclass->adskin->end_table();

        $this->ipsclass->admin->nav[] = array( 'act='.REGISTRY_URL.'&section=components&menu=categories', 'Fusion Registry Category Management' );

        // That's all folks!
        $this->ipsclass->admin->output();
    }


    /**
     * Display Fusion Registry modification category managment page
     */
    function main_menu()
    {
        $this->ipsclass->html .= $this->ipsclass->skin_acp_global->information_box( "Fusion Registry Category Management", "You may add/modify/delete Fusion Registry Modification Categories below.".'<br />&nbsp;' ) . "<br >";

        //----------------------------------------
        // Current Categories Modify/Delete Form
        //----------------------------------------
        $this->ipsclass->adskin->td_header[] = array( "Edit"   , "6%" );
        $this->ipsclass->adskin->td_header[] = array( "Delete" , "6%" );
        $this->ipsclass->adskin->td_header[] = array( "Modification Category Title"  , "88%" );

        $desc = 'These categories are used for modification classification. When you add a modification to an item the interface will ask the end user which of these categories are applicable to their mod.';

        $this->ipsclass->html .= $this->ipsclass->adskin->start_table( "Fusion Registry Modification Categories", $desc );

        $this->ipsclass->DB->query("SELECT * from ibf_registry_categories ORDER BY 'title' ASC");

        while ( $registry_cat = $this->ipsclass->DB->fetch_row() )
        {
            $this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<center><a href='".$this->ipsclass->base_url."&amp;act=".REGISTRY_URL."&section=components&menu=categories&amp;code=modify_cat_menu&amp;id={$registry_cat['id']}'>". $this->ad_registry_loader->imgs['edit'] ."</a></center>",
                                                                                 "<center><a href='".$this->ipsclass->base_url."&amp;act=".REGISTRY_URL."&section=components&menu=categories&amp;code=delete_cat_menu&amp;id={$registry_cat['id']}'>". $this->ad_registry_loader->imgs['delete'] ."</a></center>",
                                                                                 "{$registry_cat['title']}",
                                                                        )      );
        }

        $this->ipsclass->html .= $this->ipsclass->adskin->end_table();

        //------------------------------
        // Add new Category Form
        //------------------------------
        $code = "new_cat";

        $this->ipsclass->html .= $this->ipsclass->adskin->start_form( array( 1 => array( 'code', $code        ),
                                                                             2 => array( 'act' , REGISTRY_URL ),
                                                                             3 => array( 'menu', 'categories' ),
                                                                             4 => array( 'section', 'components' ),
                                                                    )      );

        $this->ipsclass->adskin->td_header[] = array( "&nbsp;"  , "20%" );
        $this->ipsclass->adskin->td_header[] = array( "&nbsp;"  , "80%" );

        $this->ipsclass->html .= $this->ipsclass->adskin->start_table( "Add New Modification Category" );

        $this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<b>Modification Category Title</b>",
                                                                             $this->ipsclass->adskin->form_input('title', '' ),
                                                                    )      );

        $this->ipsclass->html .= $this->ipsclass->adskin->end_form( "Add" );

        $this->ipsclass->html .= $this->ipsclass->adskin->end_table();

        // That's is Folks!
        $this->ipsclass->admin->output();
    }


    /**
     * Modify a modification category
     */
    function modify_cat()
    {
        // Do we have a category id?
        if ($this->ipsclass->input['id'] == "")
        {
            $this->ipsclass->admin->error("Unable to determine garage category ID");
        }

        // Grab the title of the category being modified
        $this->ipsclass->DB->query("SELECT title FROM ibf_registry_categories WHERE id='".$this->ipsclass->input['id']."'");
        $registry_cat = $this->ipsclass->DB->fetch_row();

        // Create the db update string
        $db_string = $this->ipsclass->DB->compile_db_update_string( array( 'title' => $this->ipsclass->input['title'] ) );

        $this->ipsclass->DB->query("UPDATE ibf_registry_categories SET $db_string WHERE id='".$this->ipsclass->input['id']."'");

        // Rebuild cache
        $this->update_cache();

        $text = "Modified Fusion Registry Category '{$registry_cat['title']}'";
        $url = 'act='.REGISTRY_URL.'&section=components&menu=categories';
        $time = 2;

        // Big brother is watching :)
        $this->ipsclass->admin->save_log($text);

        // End of the show, c'ya
        $this->ipsclass->admin->redirect( $url, $text, 0, $time );
    }


    /**
     * Build the HTML form for creating/modifying a modification category
     */
    function modify_cat_menu()
    {
        // Do we have a valid category id to modify?
        if ($this->ipsclass->input['id'] == "")
        {
            $this->ipsclass->admin->error("You must pass a valid Fusion Registry category id.");
        }

        // Grab the category being modified
        $this->ipsclass->DB->query("SELECT * FROM ibf_registry_categories WHERE id='".$this->ipsclass->input['id']."'");

        if ( ! $registry_cat = $this->ipsclass->DB->fetch_row() )
        {
            $this->ipsclass->admin->error("That Fusion Registry category id was not found in the database.");
        }

        // Build the HTML
        $this->ipsclass->html .= $this->ipsclass->adskin->start_form( array( 1 => array( 'code', 'modify_cat'           ),
                                                                             2 => array( 'act' , REGISTRY_URL           ),
                                                                             3 => array( 'id'  , $this->ipsclass->input['id'] ),
                                                                             4 => array( 'menu', 'categories'           ),
                                                                             5 => array( 'section', 'components'        ),
                                                                    )      );

        $this->ipsclass->html .= $this->ipsclass->skin_acp_global->information_box( "Modifying Fusion Registry Category '{$registry_cat['title']}'", "You may modify the title for the Fusion Registry category below.".'<br />&nbsp;' ) . "<br >";

        $this->ipsclass->adskin->td_header[] = array( "&nbsp;"  , "20%" );
        $this->ipsclass->adskin->td_header[] = array( "&nbsp;"  , "80%" );

        $this->ipsclass->html .= $this->ipsclass->adskin->start_table( 'Edit Category' );

        $this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "Fusion Registry Category Title",
                                                                             $this->ipsclass->adskin->form_input('title', $registry_cat['title'] ),
                                                                    )      );

        $this->ipsclass->html .= $this->ipsclass->adskin->end_form('Edit Category');

        $this->ipsclass->html .= $this->ipsclass->adskin->end_table();

        $this->ipsclass->admin->nav[] = array( 'act='.REGISTRY_URL.'&section=components&menu=categories', 'Fusion Registry Category Management' );

        // That's all folks!
        $this->ipsclass->admin->output();
    }


    /**
     * Create a new modifiction category
     */
    function new_cat()
    {
        if ($this->ipsclass->input['title'] == "")
        {
            $this->ipsclass->admin->error("You must enter a category title.");
        }

        // Create the db insert string
        $db_string = $this->ipsclass->DB->compile_db_insert_string( array( 'title' => $this->ipsclass->input['title'] ) );

        // Add the category
        $this->ipsclass->DB->query("INSERT INTO ibf_registry_categories (".$db_string['FIELD_NAMES'].") VALUES(".$db_string['FIELD_VALUES'].")");

        // Rebuild cache
        $this->update_cache();

        $text = "Added Fusion Registry Category '{$this->ipsclass->input['title']}'";
        $url = 'act='.REGISTRY_URL.'&section=components&menu=categories';
        $time = 2;

        // Big brother is watching :)
        $this->ipsclass->admin->save_log($text);

        // End of the show, c'ya
        $this->ipsclass->admin->redirect( $url, $text, 0, $time );
    }


    /**
     * Update/Rebuild cache
     *
     * This gets run any time a change is made and on demand in the ACP tools.
     */
    function update_cache()
    {
        // Start with an empty cache
        $this->ipsclass->cache['registry_categories'] = array();

        $this->ipsclass->DB->simple_construct( array( 'select' => 'id,title', 'from' => 'registry_categories' ) );
        $this->ipsclass->DB->simple_exec();

        // Loop through each setting
        while ( $row = $this->ipsclass->DB->fetch_row() )
        {
            $this->ipsclass->cache['registry_categories'][ $row['id'] ] = $row['title'];
        }

        // Save it back to the cache_store
		$this->ipsclass->update_cache( array( 'name' => 'registry_categories', 'array' => 1, 'deletefirst' => 1, 'donow' => 0 ) );
    }
}
?>